package com.yidu.TAManagement.service.impl;

import com.yidu.TAManagement.entity.TABusiness;
import com.yidu.TAManagement.mapper.TABusinessMapper;
import com.yidu.TAManagement.service.TABusinessService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class TABusinessServiceImpl implements TABusinessService {
    @Autowired
    private TABusinessMapper taBusinessMapper;


    @Override
    public int insertTABusiness(TABusiness taBusiness) {
        return taBusinessMapper.insertTABusiness(taBusiness);
    }

    @Override
    public int updateTABusiness(TABusiness taBusiness) {
        return taBusinessMapper.updateTABusiness(taBusiness);
    }

    @Override
    public int deleteTABusiness(String taId) {
        return taBusinessMapper.deleteTABusiness(taId);
    }




    /**
     * 生成SQL条件
     * @param taBusiness 实体类
     * @return  条件的字符串
     */
    public String buildStrWhere(TABusiness taBusiness){
        StringBuffer strWhere=new StringBuffer("");
        if(taBusiness.getBusinessTime()!=null && !taBusiness.getBusinessTime().equals(""))
        {
            strWhere.append(" and businessTime like '%" + taBusiness.getBusinessTime() + "%'");
        }
        if(taBusiness.getBusinessType()!=0 )
        {
            strWhere.append(" and businessType =" + taBusiness.getBusinessType() );
        }
        if(taBusiness.getBusinessState()!=0 )
        {
            strWhere.append(" and businessState =" + taBusiness.getBusinessState() );
        }
        System.out.println(strWhere);
        System.out.println("1");
        return strWhere.toString();
    }
    @Override
    public List<TABusiness> selectTABusiness(int page, int limit, TABusiness taBusiness) {
        Map taBusinessMap =new HashMap<>();
        taBusinessMap.put("start",(page-1)*limit);
        taBusinessMap.put("pageRows",limit);
        taBusinessMap.put("strWhere",buildStrWhere(taBusiness));
        List taBusinessList = taBusinessMapper.selectTABusiness(taBusinessMap);
        return taBusinessList;
    }
    @Override
    public int total(TABusiness taBusiness) {
       Map ta = new HashMap<>();
        ta.put("strWhere",buildStrWhere(taBusiness));
       int total=taBusinessMapper.total(ta);
        return total;
    }
}
